package com.oscarmovie;

import java.util.ArrayList;
import java.util.List;

import tt.util.HtmlUtils;
import tt.util.IOUtils;

public class MtimeFullcredits {

	public static void main(String[] args) {
		String url = "http://movie.mtime.com/86914/fullcredits.html";
		List<String> html = IOUtils.readHttpUrlAsList(url, "UTF-8");
		int what = 0;

		String director = "id=\"Director\"";
		String writer = "id=\"Writer\"";
		String actor = "id=\"Actor\"";
		String producer = "id=\"Produced by\"";

		String originalMusic = "id=\"Original Music\"";
		String Cinematography = "id=\"Cinematography\"";

		String start = "<ul class=\"staff_actor_list\">";
		String stop = "</ul>";

		boolean adding = false;
		List<String> directors = new ArrayList<String>();
		List<String> writers = new ArrayList<String>();
		List<String> actors = new ArrayList<String>();
		List<String> producers = new ArrayList<String>();
		for (String s : html) {
			// System.out.println("MtimeFullcredits.main(): "+s);

			if (s.indexOf(director) != -1) {
				what = 1;
			} else if (s.indexOf(writer) != -1) {
				what = 2;
			} else if (s.indexOf(actor) != -1) {
				what = 3;
			} else if (s.indexOf(producer) != -1) {
				what = 4;
			}
			int p1 = s.indexOf(start);
			if (p1 != -1) {
				adding = true;
			}

			if (adding) {
				s = clean(s);
				if (!s.equals("")) {

					if (what == 1)
						directors.add(s);
					else if (what == 2)
						writers.add(s);
					else if (what == 3)
						actors.add(s);
//					else if (what == 4)
//						producers.add(s);
				}

			}
			int p2 = s.indexOf(stop, p1);
			if (p2 != -1) {
				adding = false;
				what = 0;
			}

		}

		System.out.println("==directors======================");
		for (String s : directors) {

			// String cn = HtmlUtils.parse(s, "\">", "</a>");

			// System.out.println(cn);
			// String cn = s.substring(0,s.indexOf("&nbsp;"));
			// System.out.println(cn);
			if(s.indexOf(director)==-1){
				System.out.println(s);
			}

		}
		System.out.println("==writers======================");
		for (String s : writers) {
			if(s.indexOf(writer)==-1){
				System.out.println(s);
			}
		}
		System.out.println("==actors======================");
		for (String s : actors) {
			if(s.indexOf(actor)==-1){
				System.out.println(s);
			}
		}

//		System.out.println("==producers======================");
//		for (String s : producers) {
//			System.out.println(s);
//		}

	}

	public static String clean(String s) {
		s = s.replaceAll("<li>", "");
		s = s.replaceAll("</li>", "");
		s = s.replaceAll("</ul>", "");
		return s;
	}

}
